package com.jiantong.work.impl.service.yangzhibaoxian;

/**
 * Created by ThinkPad on 2017/9/5.
 */

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceApplicationDao;
import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceCheckDao;
import com.jiantong.work.model.yangzhibaoxian.InsuranceArea;
import com.jiantong.work.model.yangzhibaoxian.InsuranceCheck;
import com.jiantong.work.service.yangzhibaoxian.InsuranceCheckService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 保险审核表
 */
@Slf4j
@Service
@RpcProvider
public class InsuranceCheckServiceImpl implements InsuranceCheckService {
    private InsuranceCheckDao insuranceCheckDao;

    @Autowired
    public InsuranceCheckServiceImpl(InsuranceCheckDao insuranceCheckDao) {
        this.insuranceCheckDao = insuranceCheckDao;
    }
    @Override
    public Response<InsuranceCheck> findById(Long id) {
        try {
            InsuranceCheck insuranceCheck = (InsuranceCheck) insuranceCheckDao.findById(id);
            if (insuranceCheck == null) {
                log.error("insuranceCheck(id={}) is not found", id);
                return Response.fail("insuranceCheck.not.found");
            }

            return Response.ok(insuranceCheck);
        } catch (Exception e) {
            log.error("failed to find insuranceCheck(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceCheck.find.fail");
        }
    }

    @Override
    public Response<List<InsuranceCheck>> findByIds(List ids) {
        try {
            List<InsuranceCheck> insuranceCheckList =  insuranceCheckDao.findByIds(ids);
            if (insuranceCheckList == null) {
                log.error("insuranceCheckList(ids={}) is not found", ids);
                return Response.fail("insuranceCheckList.not.found");
            }

            return Response.ok(insuranceCheckList);
        } catch (Exception e) {
            log.error("failed to find insuranceCheckList(ids={}), cause:{}", ids, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceCheckList.find.fail");
        }

    }

    @Override
    public Response<List<InsuranceCheck>> list(Map map) {
        try {
            List<InsuranceCheck> insuranceCheckList = insuranceCheckDao.list(map);
            if (insuranceCheckList == null) {
                log.error("insuranceCheckList(ids={}) is not found", map);
                return Response.fail("insuranceCheckList.not.found");
            }

            return Response.ok(insuranceCheckList);
        } catch (Exception e) {
            log.error("failed to find insuranceCheckList(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceCheckList.find.fail");
        }
    }

    @Override
    public Response<Paging<InsuranceCheck>> ping(Integer offset, Integer limit, Map map) {
        try {
            PageInfo pageInfo=new PageInfo(offset,limit);
            Paging<InsuranceCheck> insuranceCheckPaging =  insuranceCheckDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), map);
            if (insuranceCheckPaging == null) {
                log.error("insuranceCheckPaging(ids={}) is not found", map);
                return Response.fail("insuranceCheckPaging.not.found");
            }

            return Response.ok(insuranceCheckPaging);
        } catch (Exception e) {
            log.error("failed to find insuranceCheckPaging(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceCheckPaging.find.fail");
        }
    }

    @Override
    public Boolean update(InsuranceCheck insuranceCheck) {
        try {
            Boolean b =  insuranceCheckDao.update(insuranceCheck);
            return b;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long create(InsuranceCheck insuranceCheck) {
        try {
            insuranceCheckDao.create( insuranceCheck);
            return  insuranceCheck.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return -500L;
        }
    }

    @Override
    public Integer creates(List<InsuranceCheck> insuranceCheckList) {
        try {

            return  insuranceCheckDao.creates(insuranceCheckList);
        } catch (Exception e) {
            e.printStackTrace();
            return -500;
        }
    }

    @Override
    public InsuranceCheck findByApplicationId(Long applicationId) {
        return insuranceCheckDao.findByApplicationId(applicationId);
    }
}
